package com.jzo2o.orders.manager.listener;

import com.alibaba.fastjson.JSON;
import com.jzo2o.api.trade.enums.TradingStateEnum;
import com.jzo2o.common.constants.MqConstants;
import com.jzo2o.common.model.msg.TradeStatusMsg;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.orders.base.enums.OrderStatusEnum;
import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.manager.porperties.TradeProperties;
import com.jzo2o.orders.manager.service.IOrdersCreateService;
import com.jzo2o.orders.manager.service.IOrdersManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * TODO
 *
 * @Author lyh
 * @Date 2024/12/10 19:54
 */
@Component
@Slf4j
public class TradeStatusUpdateListener {
    @Autowired
    private TradeProperties tradeProperties;
    @Autowired
    private IOrdersCreateService ordersCreateService;
    @Autowired
    private IOrdersManagerService ordersManagerService;
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = MqConstants.Queues.ORDERS_TRADE_UPDATE_STATUS),
            exchange = @Exchange(name = MqConstants.Exchanges.TRADE, type = ExchangeTypes.TOPIC),
            key = MqConstants.RoutingKeys.TRADE_UPDATE_STATUS
    ))
    public void listenerMsg(String msg){
        List<TradeStatusMsg> list = JSON.parseArray(msg, TradeStatusMsg.class);
        if(CollUtils.isEmpty(list)){
            return;
        }
        list.stream().filter(x->x.getProductAppId().equals(tradeProperties.getProductAppId())&&x.getStatusCode().equals(TradingStateEnum.YJS.getCode()))
                .forEach(y->ordersCreateService.paySuccess(y.getProductOrderNo(),y.getTransactionId(),y.getPaySuccessTime()));
        log.info("消费成功");
    }

    /**
     * 未支付订单超时后自动取消
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = MqConstants.Queues.ORDERS_CANCEL_QUEUE),
            exchange = @Exchange(name = MqConstants.Exchanges.ORDERS, delayed = "true"),
            key = MqConstants.RoutingKeys.ORDERS_CANCEL_KEY
    ))
    public void listenMsgForCancel(String id) {
     if(ObjectUtils.isNull(id)){
         return;
     }
        System.out.println("12345");
        String[] split = id.split(",");
        Long ordersId = Long.valueOf(split[0]);
        Long userId=null;
        if(split.length>1) {
            userId = Long.valueOf(split[1]);
        }
        Orders orders = ordersCreateService.getById(ordersId);
        if(ObjectUtils.isNull(orders)){
            return;
        }
        if(!ObjectUtils.equals(orders.getOrdersStatus(), OrderStatusEnum.NO_PAY.getStatus())){
            return;
        }
        ordersManagerService.cancelOrdersByNoPay(ordersId,userId, "未支付系统自动取消",true);
    }
}
